/*
 * tictac.h
 *
 *  Created on: 2016年8月29日
 *      Author: LongShu
 */
#ifndef GAME_TICTAC_H_
#define GAME_TICTAC_H_

#include "genlib.h"
#include "simpio.h"

/**
 * 刷新控制台
 */
#define flush() {fflush(stdout);fflush(stderr);}

/*
 * 不换行打印输出
 */
#define print(FORMAT,...) { printf(#FORMAT,##__VA_ARGS__); flush() }

/*
 * 换行打印输出
 */
#define println(FORMAT,...) { printf(#FORMAT"\n",##__VA_ARGS__); flush() }

/*
 * 求数字长度,不能是数组的指针
 */
#define sizeofArray(data) (sizeof(data)/sizeof(data[0]))

#define WinningPosition 1000
#define NeutralPosition 0
#define LosingPosition (-WinningPosition)

typedef enum {Computer, Human} playerT;

typedef int moveT;

typedef struct {
	char board[(3 * 3) + 1];
	playerT whoseTurn;
	int turnsTaken;
}*stateT;

#define MaxMoves (3 * 3)
#define MaxDepth 1000
#define FirstPlayer Computer

static int winningLines[][3] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};

static int nWinningLines = sizeofArray(winningLines);

static moveT FindBestMove(stateT state,int depth,int *pRating);
static int EvaluatePosition(stateT state,int depth);
static stateT NewGame(void);
static void DisplayGame(stateT state);
static void DisplayMove(moveT move);
static void GiveInstructions(void);
static char PlayerMark(playerT player);
static moveT GetUserMove(stateT state);
static bool MoveIsLegal(moveT move,stateT state);
static moveT ChooseComputerMove(stateT state);
static int GenerateMoveList(stateT state, moveT moveArray[]);
static void  MakeMove(stateT state, moveT move);
static void RetractMove(stateT state, moveT move);
static void AnnounceResult(stateT state);
static bool GameIsOver(stateT state);
static int EvaluateStaticPosition(stateT state);
static bool CheckForWin(stateT state, playerT player);
static playerT WhoseTurn(stateT state);
static playerT Opponent(playerT player);


/*
 * 产生随机数种子
 */
void SRANDOM() {
	//用时间做种，每次产生随机数不一样
	srand((unsigned) time(NULL));
}

/*
 * 产生随机数 [a,b]
 */
int RANDOM(int a, int b) {
	return (rand() % (b - a + 1)) + a;
}

#endif /* GAME_TICTAC_H_ */
